<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodslist" :key="i" @click="gotoGoodsDetails(item)">
        <!-- 每个商品 -->
        <view class="goods-list-item">
          <!-- 左侧栏 -->
          <view class="goods-list-item-left">
            <image :src="item.goods_small_logo||defaultPic"></image>
          </view>
          <view class="goods-list-item-right">
            <view class="goods-name">{{item.goods_name}}</view>
            <view class="goods-info-box">
              <view class="info-price">￥{{item.goods_price}}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //商品列表数据
        goodslist: [],
        //商品总数
        total: 0,
        //节流阀--防止发起多次请求
        isloading: false,
        //请求参数
        queryObj: {
          //查询关键字
          query: '',
          //分类Id
          cid: '',
          //页数索引
          pagenum: 1,
          //每页长度
          pagesize: 10
        },
        //默认空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'

      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || '';
      this.queryObj.cid = options.cid || '';
      this.getGoodsList();
    },
    methods: {
      async getGoodsList(cb) {
        this.isloading = true;
        const {
          data
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
        //console.log(data);
        if (data.meta.status === 200) {
          data.message.goods.forEach(item => {
            item.goods_price = Number(item.goods_price).toFixed(2);
          })
          //新数据和老数据进行拼接
          this.goodslist = [...this.goodslist, ...data.message.goods];
          this.total = data.message.total;
        } else {
          uni.$showMsg();
        }
        this.isloading = false;
        //关闭下拉刷新
        cb && cb();
      },
      gotoGoodsDetails(item) {
        uni.navigateTo({
          url: '/subpkg/goods_details/goods_details?goods_id=' + item.goods_id
        })
      }
    },
    //下拉触底时触发
    onReachBottom() {
      //判断数据是否已经加载完毕
      if (this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('数据加载完毕！');
      //节流阀
      if (this.isloading === true) return;
      this.queryObj.pagenum++;
      this.getGoodsList();
    },
    //下拉时刷新
    onPullDownRefresh() {
      //重置数据
      this.goodslist = [];
      this.total = 0,
        this.isloading = false,
        this.queryObj.pagenum = 1;
      //重新调用,并且关闭下拉刷新
      this.getGoodsList(() => uni.stopPullDownRefresh());
    }
  }
</script>

<style lang="scss">
  .goods-list-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;

    .goods-list-item-left {
      margin-right: 5px;

      image {
        width: 100px;
        height: 100px;
        //解决图片下方空隙
        display: block;
      }
    }

    .goods-list-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 12px;
      }

      .info-price {
        font-size: 16px;
        color: #c00000;
      }

    }
  }
</style>
